package com.kitty.mina.filter;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.chat.message.ReqChat;
import com.kitty.game.role.message.ReqCreateNewChar;
import com.kitty.game.team.message.RespMsg;
import com.kitty.game.utils.dirtywords.WordService;
import com.kitty.mina.Modules;
import com.kitty.mina.message.Message;
import com.kitty.mina.message.MessagePusher;
import lombok.extern.slf4j.Slf4j;
import org.apache.mina.core.filterchain.IoFilterAdapter;
import org.apache.mina.core.session.IoSession;
import org.nutz.lang.Strings;

import java.util.List;


/**聊天敏感词过滤*/
@Slf4j
public class ChatMessageFilter extends IoFilterAdapter {
    @Override
    public void messageReceived(NextFilter nextFilter, IoSession session, Object packet){
        Message message = (Message) packet;
        String content =null;
        if (message.getModule()== Modules.CMD_CHAT_EX){//聊天
            ReqChat reqChat = (ReqChat) message;
            content = reqChat.getMsg();
        }else if(message.getModule()== Modules.CMD_CREATE_NEW_CHAR){//创建新角色
            ReqCreateNewChar createNewChar = (ReqCreateNewChar) message;
            content = createNewChar.getChar_name();
        }/*敏感词   原注释*/
        if(!Strings.isEmpty(content)){
            List<String> set = SpringUtils.getBean(WordService.class).checkDirtyWords(content);
            if (set.size() >= 1) { //敏感词检测
                MessagePusher.pushMessage(session,new RespMsg("输入的内容存在敏感词。"));
                return;
            }
        }/*到这里*/
        nextFilter.messageReceived(session, message);
    }

}
